package com.os.controller;

import java.io.IOException;
import java.io.PipedWriter;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.os.model.FoodX;

import net.sf.json.JSONObject;

public class addFoodsControllerX extends HttpServlet{
	
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}	
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf8");
		resp.setCharacterEncoding("utf8");
		 
		HttpSession session = req.getSession();
		PrintWriter pw   = resp.getWriter();
		List<FoodX> list = (List<FoodX>) session.getAttribute("lists");
		JSONObject json = new JSONObject();
		String fids = req.getParameter("fid");
		String numStr = req.getParameter("num"); 
		int num = Integer.parseInt(numStr);
		double priceSum = Double.parseDouble(req.getParameter("priceSum"));
		int fid = Integer.parseInt(fids);
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).getFid()==fid){
				priceSum+= num*list.get(i).getFprice();
				json.put("fname", list.get(i).getFname());
				json.put("fprice", list.get(i).getFprice());
				json.put("fid", fid);
				json.put("priceSum", priceSum);
				pw.write(json.toString());
			}
		}
		pw.flush();pw.close();
	}
}
